home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-10 | 6.1 KB | 246 lines | [TEXT/CWIE] |
- // ===========================================================================
- // LGroupBox.cp ©1995-1996 Metrowerks Inc. All rights reserved.
- // ===========================================================================
-
- #ifdef PowerPlant_PCH
- #include PowerPlant_PCH
- #endif
-
- #include <LGroupBox.h>
- #include <UDrawingState.h>
- #include <UDrawingUtils.h>
- #include <UTextTraits.h>
-
-
- // ———————————————————————————————————————————————————————————————————————————
- // • CreateGroupBoxStream
- // ———————————————————————————————————————————————————————————————————————————
- // Create a new GroupBox object from the data in a Stream
-
- LGroupBox*
- LGroupBox::CreateGroupBoxStream(
- LStream* inStream)
- {
- return new LGroupBox(inStream);
- }
-
-
- // ———————————————————————————————————————————————————————————————————————————
- // • LGroupBox
- // ———————————————————————————————————————————————————————————————————————————
- // Default Constructor
-
- LGroupBox::LGroupBox()
- {
- mFrameColor.red = mFrameColor.blue = mFrameColor.green = 0x8000;
- }
-
-
- // ———————————————————————————————————————————————————————————————————————————
- // • LGroupBox(const LGroupBox&)
- // ———————————————————————————————————————————————————————————————————————————
- // Copy Constructor
-
- LGroupBox::LGroupBox(
- const LGroupBox &inGroupBox)
- : LCaption(inGroupBox)
- {
- mFrameColor.red = mFrameColor.blue = mFrameColor.green = 0x8000;
- }
-
-
- // ———————————————————————————————————————————————————————————————————————————
- // • LGroupBox(SPaneInfo&, Str255, ResIDT)
- // ———————————————————————————————————————————————————————————————————————————
- // Construct from parameters. Use this constructor to create a GroupBox
- // from runtime data.
-
- LGroupBox::LGroupBox(
- const SPaneInfo& inPaneInfo,
- Str255 inString,
- ResIDT inTextTraitsID)
- : LCaption(inPaneInfo, inString, inTextTraitsID)
- {
- mFrameColor.red = mFrameColor.blue = mFrameColor.green = 0x8000;
- }
-
-
- // ———————————————————————————————————————————————————————————————————————————
- // • LGroupBox(LStream*)
- // ———————————————————————————————————————————————————————————————————————————
- // Construct from data in a Stream
-
- LGroupBox::LGroupBox(
- LStream *inStream)
- : LCaption(inStream)
- {
- mFrameColor.red = mFrameColor.blue = mFrameColor.green = 0x2000;
- }
-
-
- // ———————————————————————————————————————————————————————————————————————————
- // • ~LGroupBox
- // ———————————————————————————————————————————————————————————————————————————
- // Destructor
-
- LGroupBox::~LGroupBox()
- {
- }
-
-
- // ———————————————————————————————————————————————————————————————————————————
- // • DrawSelf
- // ———————————————————————————————————————————————————————————————————————————
- // Draw GroupBox
- //
- // We draw in two stages. First we draw the frame rectangle,
- // minus the area where the title text is drawn; then we draw the
- // title text. In order to draw without any flashing, we find the
- // frame of the text box and clip it out of the drawing region.
- //
- void
- LGroupBox::DrawSelf()
- {
- Rect textFrame;
- CalcTextBoxFrame(textFrame);
-
- RgnHandle newClip = ::NewRgn();
- ThrowIfNil_(newClip);
- ::GetClip(newClip);
-
- RgnHandle textRgn = ::NewRgn();
- ThrowIfNil_(textRgn);
- ::RectRgn(textRgn, &textFrame);
-
- ::DiffRgn(newClip, textRgn, newClip);
-
- {
- StClipRgnState clip(newClip);
-
- Rect borderFrame;
- CalcLocalFrameRect(borderFrame);
- if (textFrame.right > textFrame.left)
- borderFrame.top += (textFrame.bottom - textFrame.top) >> 1;
-
- DrawBorder(borderFrame);
- }
-
- ::PenNormal();
- DrawText(textFrame);
-
- ::DisposeRgn (newClip);
- ::DisposeRgn (textRgn);
- }
-
-
- // ———————————————————————————————————————————————————————————————————————————
- // • DrawText
- // ———————————————————————————————————————————————————————————————————————————
- // Draw the text given the placement chosen by CalcTextBoxFrame
-
- void
- LGroupBox::DrawText(
- const Rect &inRect)
- {
- // Retrieve info about font again.
-
- Int16 just = UTextTraits::SetPortTextTraits(mTxtrID);
- FontInfo fInfo;
- ::GetFontInfo(&fInfo);
-
- // Set background color
-
- RGBColor textColor; // Text has its own foreground color
- ::GetForeColor(&textColor);
-
- ApplyForeAndBackColors();
- ::RGBForeColor(&textColor);
-
- ::MoveTo(inRect.left + 3, inRect.top + fInfo.ascent + (fInfo.leading >> 1));
- ::DrawString(mText);
-
- }
-
-
- // ———————————————————————————————————————————————————————————————————————————
- // • DrawBorder
- // ———————————————————————————————————————————————————————————————————————————
- // Draw border around the GroupBox
-
- void
- LGroupBox::DrawBorder(
- const Rect &inRect)
- {
- StDeviceLoop devices(inRect);
- Int16 depth;
-
- ::PenNormal();
-
- while (devices.NextDepth(depth)) {
- StColorPenState savePenState; // Will save and restore pen state
- if (depth > 2) {
- ::RGBForeColor(&mFrameColor);
- } else {
- ::PenPat(&UQDGlobals::GetQDGlobals()->gray);
- }
- ::FrameRect(&inRect);
- }
- }
-
-
- // ———————————————————————————————————————————————————————————————————————————
- // • CalcTextBoxFrame
- // ———————————————————————————————————————————————————————————————————————————
- // Decides where the text will be drawn, but does not draw the text
-
- void
- LGroupBox::CalcTextBoxFrame(
- Rect &outRect)
- {
- // Find the edges of the pane.
-
- CalcLocalFrameRect(outRect);
-
-
- // If text is empty, return an empty rectangle.
-
- if (mText[0] == 0) {
- outRect.right = outRect.left;
- outRect.bottom = outRect.top;
- return;
- }
-
-
- // We have some text, find its size and place it.
- // This version is pretty naive; it assumes the text will
- // fit comfortably on a single line.
-
- Int16 just = UTextTraits::SetPortTextTraits(mTxtrID);
- Int16 strSize = ::StringWidth(mText) + 6; // 3 pixels slop on either side
- Int16 frameWidth = outRect.right - outRect.left;
-
- FontInfo fInfo; // vertical placement is always at top
- ::GetFontInfo(&fInfo);
- outRect.bottom = outRect.top + fInfo.ascent + fInfo.descent + fInfo.leading;
-
- switch (just) { // horizontal placement depends on Txtr
-
- case teCenter:
- outRect.left += (frameWidth - strSize) >> 1;
- outRect.right = outRect.left + strSize;
- break;
-
- case teFlushRight:
- outRect.right -= 6;
- outRect.left = outRect.right - strSize;
- break;
-
- case teFlushDefault:
- case teFlushLeft:
- default:
- outRect.left += 6;
- outRect.right = outRect.left + strSize;
- break;
- }
- }
-